package com.njsoft.mydemotest;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * Created by cuien on 2018/2/3.
 */

public class BottomDialog {
    private Context context;
    private AlertDialog dialog;

    private String TAG = "jpush-->";

    public BottomDialog(Context context) {
        this.context = context;
    }
    public interface MyDialogListener {
        void setChoosePhotoClick();
        void setTakePhotoClick();
    }
    MyDialogListener myDialogListener;
    //初始化
    public void setBottomDialogListener( MyDialogListener listener) {
        myDialogListener = listener;
        View view = LayoutInflater.from(context).inflate(R.layout.good_attribute_select_dialog, null);
        dialog = new AlertDialog.Builder(context, R.style.ActionSheetDialogStyle).create();
        TextView choosePhoto = (TextView) view.findViewById(R.id.choosePhoto);
        TextView takePhoto = (TextView) view.findViewById(R.id.takePhoto);
        dialog.setView(view);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity( Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 20;//设置Dialog距离底部的距离
//       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        choosePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialogListener.setChoosePhotoClick();
                dialog.dismiss();
            }
        });
        takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialogListener.setTakePhotoClick();
                dialog.dismiss();
            }
        });
    }

    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
